package com.gujiangbo.volatiles;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;

public class VolatileTest {

    private static int a = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[100];
        CountDownLatch latch = new CountDownLatch(threads.length);

        Object obj = new Object();

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                synchronized (obj) {
                    for (int j = 0; j < 10000; j++) {
                        a++;
                    }
                    latch.countDown();
                }

            });
        }

        Arrays.asList(threads).forEach(v -> v.start());
        latch.await();
        System.out.println(a);
    }
}
